// 学院管理
import {
    collegeGet,
    collegeDelete,
    collegeBatchDelete,
    collegeAdd,
    getAdminInfo
} from "../../api/college.js";


const state = () => ({
    collegeList: [],// 存储学院列表数据
    collegeNameId:[]
})

const mutations = { // mutations改变state中的数据
    SET_COLLEGE_LIST: (state, collegeList) => { // 存储学院数据
        state.collegeList = collegeList
    },
    SET_COLLEGE:(state,collegeList)=>{
        let lst=[]
        for(let i=0;i<collegeList.length;i++){
            lst.push({"collegeId":collegeList[i].collegeId,"name":collegeList[i].name})
        }
        state.collegeNameId=lst;
    },
    DEL_COLLEGE: (state, collegeId) => { // 删除学院
        for(var i = 0; i < state.collegeList.length; i ++){
            if(state.collegeList[i].collegeId === collegeId){
                state.collegeList.splice(i, 1)
                break;
            }
        }
    },
    ADD_COLLEGE: (state, data) => { // 添加学院
        state.collegeList.push({key:data.collegeId,collegeId:data.collegeId, name:data.name})
    },
    NULL:()=>{}

}

const actions = { // 发送请求
    getCollegeList({commit},data) { // 获取学院列表
        return new Promise((resolve, reject) => {
            collegeGet(data).then((data) => {
                if(data.code === 1){
                    commit('SET_COLLEGE_LIST', data.data)
                    commit('SET_COLLEGE', data.data)//设置学院
                    resolve(data)
                }else{
                    reject(data)
                }
            })
        })
    },
    collegeDelete({commit}, data){ // 删除学院
        const collegeId = data.collegeId;
        // console.log('删除学院id',data)
        // console.log('删除学院id',collegeId)
        return new Promise((resolve, reject) => {
            collegeDelete(data).then((data) => {
                if(data.code === 1){
                    commit('DEL_COLLEGE', collegeId)
                    resolve(data)
                }else{
                    reject(data)
                }
            })
        })
    },
    collegeBatchDelete({commit}, data){ // 批量删除学院
        const arrCollegeId = [];
        this.arrCollegeId = data.collegeIds
        return new Promise((resolve, reject) => {
            collegeBatchDelete(data).then((data) => {
                if(data.code === 1){
                    for(let i = 0; i < arrCollegeId.length; i ++){
                        commit('DEL_COLLEGE', arrCollegeId[i])
                    }

                    resolve(data)
                }else{
                    reject(data)
                }
            })
        })
    },
    collegeAdd({commit}, data){ // 添加学院
        const collegeName = data.name
        return new Promise((resolve, reject) => {
            collegeAdd(data).then((data) => {
                if(data.code === 1){
                    commit('ADD_COLLEGE', {name:collegeName,collegeId:data.data})
                    resolve(data)
                }else{
                    reject(data)
                }
            })
        })
    },

    getAdminInfo({commit},data){//获取指定管理员信息
        return new Promise((resolve, reject) => {
            getAdminInfo(data).then((data) => {
                if(data.code === 1){
                    commit('NULL');
                    resolve(data)
                }else{
                    reject(data)
                }
            })
        })
    }
}


export default {
    namespaced: true,
    state,
    mutations,
    actions
}
